<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>webshell的实现流程 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>webshell的实现流程</h1>
  <time datetime=2020-06-02T16:31:24&#43;0800 class="post-date">Tue, Jun 2, 2020</time>
  <h4 id="什么是webshell">什么是webshell</h4>
<p>​	“web”的含义是显然需要服务器开放web服务，“shell”的含义是取得对服务器某种程度上操作权限。webshell常常被称为通过网站端口对网站服务器的某种程度上操作的权限。</p>
<p><strong>vue端</strong></p>
<p><em><strong>1、安装</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue"><span style="color:#75715e">//指定版本安装，最新版的xterm文件的改动很大，使用下面的方法会报错
</span><span style="color:#75715e"></span><span style="color:#a6e22e">npm</span> <span style="color:#a6e22e">install</span> <span style="color:#a6e22e">xterm</span><span style="color:#960050;background-color:#1e0010">@</span><span style="color:#ae81ff">3.1.0</span> <span style="color:#f92672">--</span><span style="color:#a6e22e">save</span>
</code></pre></div><p><em><strong>2、main中导包</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#66d9ef">import</span> <span style="color:#e6db74">&#39;xterm/dist/xterm.css&#39;</span>;
<span style="color:#66d9ef">import</span> { <span style="color:#a6e22e">Terminal</span> } <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;xterm&#39;</span>;

<span style="color:#75715e">// 布局组件 可以不用
</span><span style="color:#75715e"></span><span style="color:#66d9ef">import</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">as</span> <span style="color:#a6e22e">fit</span> <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;xterm/lib/addons/fit/fit&#39;</span>;
<span style="color:#a6e22e">Terminal</span>.<span style="color:#a6e22e">applyAddon</span>(<span style="color:#a6e22e">fit</span>);
</code></pre></div><p><em><strong>3、在页面中的显示</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-javascript" data-lang="javascript"><span style="color:#f92672">&lt;</span><span style="color:#a6e22e">template</span><span style="color:#f92672">&gt;</span>
    <span style="color:#f92672">&lt;</span><span style="color:#a6e22e">div</span><span style="color:#f92672">&gt;</span>
        <span style="color:#f92672">&lt;</span><span style="color:#a6e22e">div</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;terminal&#34;</span> <span style="color:#a6e22e">style</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;width: 500px;height:300px;&#34;</span><span style="color:#f92672">&gt;&lt;</span><span style="color:#960050;background-color:#1e0010">/div&gt;</span>
    <span style="color:#f92672">&lt;</span><span style="color:#960050;background-color:#1e0010">/div&gt;</span>
<span style="color:#f92672">&lt;</span><span style="color:#960050;background-color:#1e0010">/template&gt;</span>
</code></pre></div><p><em><strong>4.数据操作</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue"><span style="color:#a6e22e">mounted</span> () {

      <span style="color:#75715e">//实例化一个websocket，用于和django江湖
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">terminalSocket</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">WebSocket</span>(<span style="color:#e6db74">&#34;ws://127.0.0.1:8000/web/&#34;</span>);
      <span style="color:#75715e">//获取到后端传回的信息
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">terminalSocket</span>.<span style="color:#a6e22e">onmessage</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">res</span>) =&gt; {
          <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">data</span>);
          <span style="color:#75715e">// var message = JSON.parse(res.data);
</span><span style="color:#75715e"></span>          <span style="color:#75715e">//将传回来的数据显示在xterm里
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">writeln</span>(<span style="color:#e6db74">&#34;\r\n&#34;</span><span style="color:#f92672">+</span><span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">data</span>);
          <span style="color:#75715e">//重置要发送的信息
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>
          <span style="color:#75715e">//换行，显示下一个开头
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#34;\r\n$ &#34;</span>);
      }
    <span style="color:#75715e">//ws连接的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onopen = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;websocket is Connected...&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//ws关闭的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onclose = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;websocket is Closed...&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//ws错误的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onerror = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;damn Websocket is broken!&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.term.attach(this.terminalSocket)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// 绑定xterm到ws流中 },
</span><span style="color:#75715e"></span>      

      
      <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">terminalContainer</span> <span style="color:#f92672">=</span> document.<span style="color:#a6e22e">getElementById</span>(<span style="color:#e6db74">&#39;terminal&#39;</span>)
      <span style="color:#75715e">//创建xterm实例
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">Terminal</span>({
        <span style="color:#a6e22e">cursorBlink</span><span style="color:#f92672">:</span> <span style="color:#66d9ef">true</span>, <span style="color:#75715e">// 显示光标
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">cursorStyle</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#34;underline&#34;</span> <span style="color:#75715e">// 光标样式
</span><span style="color:#75715e"></span>        })                     <span style="color:#75715e">// 创建一个新的Terminal对象
</span><span style="color:#75715e"></span>      
      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">open</span>(<span style="color:#a6e22e">terminalContainer</span>)              <span style="color:#75715e">// 将term挂载到dom节点上
</span><span style="color:#75715e"></span>    },
</code></pre></div><p><em><strong>vue端完整代码</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-vue" data-lang="vue">&lt;<span style="color:#f92672">template</span>&gt;
  &lt;<span style="color:#f92672">div</span> <span style="color:#a6e22e">class</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;console&#34;</span> <span style="color:#a6e22e">id</span><span style="color:#f92672">=</span><span style="color:#e6db74">&#34;terminal&#34;</span>&gt;&lt;/<span style="color:#f92672">div</span>&gt;
&lt;/<span style="color:#f92672">template</span>&gt;
&lt;<span style="color:#f92672">script</span>&gt;
    <span style="color:#66d9ef">import</span> { <span style="color:#a6e22e">Terminal</span> } <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;xterm&#39;</span>
    <span style="color:#66d9ef">import</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">as</span> <span style="color:#a6e22e">attach</span> <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;xterm/lib/addons/attach/attach&#39;</span>
    <span style="color:#66d9ef">import</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">as</span> <span style="color:#a6e22e">fit</span> <span style="color:#a6e22e">from</span> <span style="color:#e6db74">&#39;xterm/lib/addons/fit/fit&#39;</span>
  <span style="color:#66d9ef">export</span> <span style="color:#66d9ef">default</span> {
    <span style="color:#a6e22e">name</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#39;webssh&#39;</span>,
    <span style="color:#a6e22e">data</span> () {
      <span style="color:#66d9ef">return</span> {
        
        <span style="color:#a6e22e">term</span><span style="color:#f92672">:</span> <span style="color:#66d9ef">null</span>,
        <span style="color:#a6e22e">terminalSocket</span><span style="color:#f92672">:</span> <span style="color:#66d9ef">null</span>,
        <span style="color:#a6e22e">order</span><span style="color:#f92672">:</span><span style="color:#e6db74">&#39;&#39;</span>
      }
    },
    <span style="color:#a6e22e">methods</span><span style="color:#f92672">:</span> {
      
      
    },
    <span style="color:#a6e22e">mounted</span> () {

      <span style="color:#75715e">//实例化一个websocket，用于和django江湖
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">terminalSocket</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">WebSocket</span>(<span style="color:#e6db74">&#34;ws://127.0.0.1:8000/web/&#34;</span>);
      <span style="color:#75715e">//获取到后端传回的信息
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">terminalSocket</span>.<span style="color:#a6e22e">onmessage</span> <span style="color:#f92672">=</span> (<span style="color:#a6e22e">res</span>) =&gt; {
          <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">data</span>);
          <span style="color:#75715e">// var message = JSON.parse(res.data);
</span><span style="color:#75715e"></span>          <span style="color:#75715e">//将传回来的数据显示在xterm里
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">writeln</span>(<span style="color:#e6db74">&#34;\r\n&#34;</span><span style="color:#f92672">+</span><span style="color:#a6e22e">res</span>.<span style="color:#a6e22e">data</span>);
          <span style="color:#75715e">//重置要发送的信息
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;&#34;</span>
          <span style="color:#75715e">//换行，显示下一个开头
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#34;\r\n$ &#34;</span>);
      }
    <span style="color:#75715e">//ws连接的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onopen = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;websocket is Connected...&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//ws关闭的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onclose = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;websocket is Closed...&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//ws错误的时候
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.terminalSocket.onerror = function(){
</span><span style="color:#75715e"></span>    <span style="color:#75715e">//     console.log(&#39;damn Websocket is broken!&#39;)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// }
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// this.term.attach(this.terminalSocket)
</span><span style="color:#75715e"></span>    <span style="color:#75715e">// 绑定xterm到ws流中 },
</span><span style="color:#75715e"></span>      

      
      <span style="color:#66d9ef">let</span> <span style="color:#a6e22e">terminalContainer</span> <span style="color:#f92672">=</span> document.<span style="color:#a6e22e">getElementById</span>(<span style="color:#e6db74">&#39;terminal&#39;</span>)
      <span style="color:#75715e">//创建xterm实例
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> <span style="color:#a6e22e">Terminal</span>({
        <span style="color:#a6e22e">cursorBlink</span><span style="color:#f92672">:</span> <span style="color:#66d9ef">true</span>, <span style="color:#75715e">// 显示光标
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">cursorStyle</span><span style="color:#f92672">:</span> <span style="color:#e6db74">&#34;underline&#34;</span> <span style="color:#75715e">// 光标样式
</span><span style="color:#75715e"></span>        })                     <span style="color:#75715e">// 创建一个新的Terminal对象
</span><span style="color:#75715e"></span>      
      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">open</span>(<span style="color:#a6e22e">terminalContainer</span>)              <span style="color:#75715e">// 将term挂载到dom节点上
</span><span style="color:#75715e"></span>
      
      <span style="color:#75715e">//在xterm上显示命令行提示
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#39;$ &#39;</span>)
      <span style="color:#75715e">//监听xterm的键盘事件
</span><span style="color:#75715e"></span>      <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">on</span>(<span style="color:#e6db74">&#39;key&#39;</span>, (<span style="color:#a6e22e">key</span>, <span style="color:#a6e22e">ev</span>)=&gt;{ 
        <span style="color:#75715e">// key是输入的字符 ev是键盘按键事件
</span><span style="color:#75715e"></span>        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;key==========&#34;</span>, <span style="color:#a6e22e">ev</span>.<span style="color:#a6e22e">keyCode</span>);
        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#a6e22e">key</span>) <span style="color:#75715e">// 将输入的字符打印到黑板中
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">if</span> (<span style="color:#a6e22e">ev</span>.<span style="color:#a6e22e">keyCode</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">13</span>) { <span style="color:#75715e">// 输入回车
</span><span style="color:#75715e"></span>            <span style="color:#75715e">// console.log(&#34;输入回车&#34;)
</span><span style="color:#75715e"></span>            <span style="color:#75715e">// this.term.write(&#39;$ &#39;)
</span><span style="color:#75715e"></span>            <span style="color:#75715e">// console.log(this.order)
</span><span style="color:#75715e"></span>            
            <span style="color:#75715e">//使用webscoket将数据发送到django
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">terminalSocket</span>.<span style="color:#a6e22e">send</span>(<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>)
            <span style="color:#75715e">// this.order=&#39;&#39;
</span><span style="color:#75715e"></span>            <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;里面的order&#34;</span>,<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>)
        }<span style="color:#66d9ef">else</span> <span style="color:#66d9ef">if</span>(<span style="color:#a6e22e">ev</span>.<span style="color:#a6e22e">keyCode</span> <span style="color:#f92672">==</span> <span style="color:#ae81ff">8</span>){<span style="color:#75715e">//删除按钮
</span><span style="color:#75715e"></span>          <span style="color:#75715e">//截取字符串[0,lenth-1]
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span> <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>.<span style="color:#a6e22e">substr</span>(<span style="color:#ae81ff">0</span>,<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>.<span style="color:#a6e22e">length</span><span style="color:#f92672">-</span><span style="color:#ae81ff">1</span>)

          <span style="color:#75715e">//清空当前一条的命令
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#34;\x1b[2K\r&#34;</span>)
          <span style="color:#75715e">//简化当前的新的命令显示上
</span><span style="color:#75715e"></span>          <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">term</span>.<span style="color:#a6e22e">write</span>(<span style="color:#e6db74">&#34;$ &#34;</span><span style="color:#f92672">+</span><span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>)

          <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;截取的字符串&#34;</span><span style="color:#f92672">+</span><span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>)
          <span style="color:#66d9ef">typeof</span> <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>
        }<span style="color:#66d9ef">else</span>{<span style="color:#75715e">// 将每次输入的字符拼凑起来
</span><span style="color:#75715e"></span>        <span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span> <span style="color:#f92672">+=</span> <span style="color:#a6e22e">key</span>
        <span style="color:#a6e22e">console</span>.<span style="color:#a6e22e">log</span>(<span style="color:#e6db74">&#34;外面的order&#34;</span>,<span style="color:#66d9ef">this</span>.<span style="color:#a6e22e">order</span>)}
        
    })
    },
     
  }
&lt;/<span style="color:#f92672">script</span>&gt;
</code></pre></div><p><strong>dajngo端</strong></p>
<p><em><strong>1、安装</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">pip install paramiko
</code></pre></div><p><em><strong>2.利用<code>paramiko</code>进行<code>ssh</code>远程登录</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">import</span> paramiko

<span style="color:#75715e"># 创建SSH客户端</span>
client <span style="color:#f92672">=</span> paramiko<span style="color:#f92672">.</span>SSHClient()
<span style="color:#75715e"># SSH客户端Host策略,目的是接受不在本地Known_host文件下的主机。</span>
client<span style="color:#f92672">.</span>set_missing_host_key_policy(paramiko<span style="color:#f92672">.</span>AutoAddPolicy())
<span style="color:#75715e"># SSH客户端开启连接</span>
client<span style="color:#f92672">.</span>connect(
    hostname<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;192.168.184.144&#34;</span>,port<span style="color:#f92672">=</span><span style="color:#ae81ff">22</span>,username<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;xterm&#39;</span>,password<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;123123&#39;</span>
)
<span style="color:#75715e"># stdout 为正确输出，stderr为错误输出,每次执行完成只有一个变量中有值</span>
stdin, stdout, stderr <span style="color:#f92672">=</span> client<span style="color:#f92672">.</span>exec_command(<span style="color:#e6db74">&#34;ls&#34;</span>)

<span style="color:#75715e"># 执行ls命令的结果</span>
<span style="color:#66d9ef">print</span>(stdout<span style="color:#f92672">.</span>read()<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#39;utf-8&#39;</span>))
</code></pre></div><p><em><strong>3.结合<code>websocket</code>实现实时操作</strong></em></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#75715e">#wsserver.py</span>
<span style="color:#f92672">from</span> channels.generic.websocket <span style="color:#f92672">import</span> WebsocketConsumer
<span style="color:#f92672">import</span> paramiko
<span style="color:#66d9ef">class</span> <span style="color:#a6e22e">WebSSHService</span>(WebsocketConsumer):

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">connect</span>(self):
        self<span style="color:#f92672">.</span>accept()
        self<span style="color:#f92672">.</span>sh <span style="color:#f92672">=</span> paramiko<span style="color:#f92672">.</span>SSHClient()  <span style="color:#75715e"># 1 创建SSH对象</span>
        self<span style="color:#f92672">.</span>sh<span style="color:#f92672">.</span>set_missing_host_key_policy(paramiko<span style="color:#f92672">.</span>AutoAddPolicy())  <span style="color:#75715e"># 2 允许连接不在know_hosts文件中的主机</span>
        self<span style="color:#f92672">.</span>sh<span style="color:#f92672">.</span>connect(<span style="color:#e6db74">&#34;192.168.184.144&#34;</span>, username<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;root&#34;</span>, password<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;beijing2018&#34;</span>)  <span style="color:#75715e"># 3 连接服务器</span>
        <span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;连接成功&#34;</span>)

    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">receive</span>(self, text_data<span style="color:#f92672">=</span>None, bytes_data<span style="color:#f92672">=</span>None):
        <span style="color:#66d9ef">print</span>(str(text_data))  <span style="color:#75715e"># 打印收到的数据</span>
        <span style="color:#66d9ef">print</span>(type(text_data))

        stdin, stdout, stderr <span style="color:#f92672">=</span> self<span style="color:#f92672">.</span>sh<span style="color:#f92672">.</span>exec_command(text_data)

        right_info <span style="color:#f92672">=</span> stdout<span style="color:#f92672">.</span>read()
        err_info <span style="color:#f92672">=</span> stderr<span style="color:#f92672">.</span>read()
        <span style="color:#66d9ef">print</span>(right_info)

        <span style="color:#66d9ef">if</span> right_info:
            new_data <span style="color:#f92672">=</span> right_info<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#34;utf-8&#34;</span>)<span style="color:#f92672">.</span>replace(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>,<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\r\n</span><span style="color:#e6db74">&#34;</span>)
            <span style="color:#66d9ef">print</span>(new_data)
            self<span style="color:#f92672">.</span>send(new_data)
        <span style="color:#66d9ef">elif</span> err_info:
            new_data <span style="color:#f92672">=</span> err_info<span style="color:#f92672">.</span>decode(<span style="color:#e6db74">&#34;utf-8&#34;</span>)<span style="color:#f92672">.</span>replace(<span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>, <span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\r\n</span><span style="color:#e6db74">&#34;</span>)
            <span style="color:#66d9ef">print</span>(new_data)
            self<span style="color:#f92672">.</span>send(new_data)
        <span style="color:#66d9ef">else</span>:
            <span style="color:#66d9ef">print</span>(self<span style="color:#f92672">.</span>send(<span style="color:#e6db74">&#34;命令执行成功&#34;</span>))


    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">disconnect</span>(self, code):
        <span style="color:#66d9ef">print</span>(f<span style="color:#e6db74">&#39;sorry{self},你被女朋友抛弃了&#39;</span>)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
